package com.itheima.xx.e_request_01;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

/**
 * Description:
 *
 * @author codermao
 * @date 2023/11/1 10:49
 */
@WebServlet("/req1")
public class RequestDemo01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*
            请求行包含三块内容，分别是`请求方式`、`请求资源路径`、`HTTP协议及版本`
         */

        /*// String getMethod()：获取请求方式： GET
        String method = req.getMethod();
        //GET
        System.out.println(method);
        // String getContextPath()：获取虚拟目录(项目访问路径)：/request-demo
        String contextPath = req.getContextPath();
        System.out.println(contextPath);
        // StringBuffer getRequestURL(): 获取URL(统一资源定位符)：http://localhost:8080/request-demo/req1
        StringBuffer url = req.getRequestURL();
        System.out.println(url.toString());
        // String getRequestURI()：获取URI(统一资源标识符)： /request-demo/req1
        String uri = req.getRequestURI();
        System.out.println(uri);
        // String getQueryString()：获取请求参数（GET方式）： username=zhangsan
        String queryString = req.getQueryString();
        System.out.println(queryString);*/

        /*
            获取请求头参数
         */

        String agent = req.getHeader("user-agent");
        System.out.println(agent);

        /*
            获取请求体参数
         */

        BufferedReader br = req.getReader();
        String line = br.readLine();
        System.out.println(line);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
